<html>
<head><meta charset="utf-8"><title>Hardcoding Watt Support · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html">Hardcoding Watt Support</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="180906401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180906401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180906401">(Nov 16 2019 at 10:14)</a>:</h4>
<p>Would it be a worthwhile goal to hardcode support for <a href="https://github.com/dtolnay/watt" target="_blank" title="https://github.com/dtolnay/watt">Watt</a> procedural macros. Conceivably, we could match on the 'standard' pattern shown in the readme (with different names), although this could be somewhat brittle.<br>
This would have advantages in terms of allowing some basic proc macro support, without needing the full compiler toolchain setup. Additionally, these proc macros are almost certainly <em>guaranteed</em> to be deterministic, which is good for salsa.</p>



<a name="180906465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180906465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180906465">(Nov 16 2019 at 10:16)</a>:</h4>
<p>I definitely think that some form of Watt is a promising way to handle proc macros. Didn't have a change to thing this through yet</p>



<a name="180906600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180906600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180906600">(Nov 16 2019 at 10:21)</a>:</h4>
<p>I'm just trying to work out what the actual API surface for the tokenstream passed into webassembly looks like</p>



<a name="180906655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180906655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180906655">(Nov 16 2019 at 10:22)</a>:</h4>
<p><span class="user-mention" data-user-id="200146">@Daniel Mcnab</span> isn't it just <a href="https://docs.rs/crate/proc-macro2/1.0.6" target="_blank" title="https://docs.rs/crate/proc-macro2/1.0.6">https://docs.rs/crate/proc-macro2/1.0.6</a> ?</p>



<a name="180906712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180906712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180906712">(Nov 16 2019 at 10:24)</a>:</h4>
<p>In theory, but I'm just trying to work out how we would actually construct one to pass into wasm</p>



<a name="180907035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180907035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180907035">(Nov 16 2019 at 10:34)</a>:</h4>
<p>I suppose we could actually entirely reuse Watt's implementation if we could work out how to make a <code>proc_macro::TokenStream</code>.<br>
Ahah - <a href="https://doc.rust-lang.org/proc_macro/struct.TokenStream.html" target="_blank" title="https://doc.rust-lang.org/proc_macro/struct.TokenStream.html"><code>impl From&lt;TokenTree&gt; for TokenStream</code></a> exists.</p>



<a name="180907113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180907113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180907113">(Nov 16 2019 at 10:37)</a>:</h4>
<p>Although I don't think we already have the conversions to <code>proc_macro::TokenTree</code>, so those would need to be written too.</p>



<a name="180907649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180907649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180907649">(Nov 16 2019 at 10:54)</a>:</h4>
<p>Maybe for the starter, convert TokenTree to text and parse that text to TokenStream ?</p>



<a name="180907707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180907707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180907707">(Nov 16 2019 at 10:56)</a>:</h4>
<p>Our tokens have ids, so I don't think that building a proper bridge betwen wasm and host would be difficult -- just a question of consrcuting the required side-tables</p>



<a name="180907711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180907711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180907711">(Nov 16 2019 at 10:56)</a>:</h4>
<p>Or, somewhat equivalent, it's trivial to impl Ser/De for TokenStream</p>



<a name="180915974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180915974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180915974">(Nov 16 2019 at 15:20)</a>:</h4>
<p>So just to confirm - this is something which is at least theoretically desirable?<br>
I'm going to start hacking on it, but may need some help understanding how to integrate it and when to expand them.  I think the implementation itself should be fairly easy, although I suppose I also need to work out what crate/it should go in or if it should go in a new crate. <br>
I presume the first step is to ensure  proc macros are in the macro namespace, based on collecting proc_macro attributes?<br>
Does anyone know if proc macro derives/attribute macros share the same macro namespace as bang macros?</p>



<a name="180916317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180916317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180916317">(Nov 16 2019 at 15:29)</a>:</h4>
<p>Yeah, running proc-macro2 proc-macros via wasm is probably more promising than external process. The hard bit here is integration with cargo</p>



<a name="180916439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180916439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180916439">(Nov 16 2019 at 15:33)</a>:</h4>
<p>Macros which use Watt are already compiled to wasm, which makes our lives easier. What I'm hacking on at the moment is to only add support for watt macros, as a stopgap to experiment. That is - see how a full proc macro expansion would implement with our existing infra  and to try and discover if wasm macros could have any unforeseen issues for us (which I doubt)</p>



<a name="180916453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180916453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180916453">(Nov 16 2019 at 15:33)</a>:</h4>
<p>As a bonus, it might be worthwhile trying to remove syn entirely from our direct dependency graph</p>



<a name="180916571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180916571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180916571">(Nov 16 2019 at 15:36)</a>:</h4>
<p>But that is a separate, loosely related experiment.</p>



<a name="180916858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180916858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180916858">(Nov 16 2019 at 15:45)</a>:</h4>
<p>It might also be worth seeing what <span class="user-mention silent" data-user-id="119235">David Tolnay</span> (silent mention)  thinks about this, as the author of watt.</p>



<a name="180961027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180961027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180961027">(Nov 17 2019 at 15:16)</a>:</h4>
<p>OK - I've been hacking on this but have hit a snag at <code>impl HasSource&lt;Ast=ast::MacroCall&gt; for MacroDef</code>, because I've changed MacroDefId to support procedural macros, which only have an <code>ast::FnDef</code>.</p>



<a name="180961043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180961043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180961043">(Nov 17 2019 at 15:17)</a>:</h4>
<p>Hm, yeah, I guess <code>HasSource</code> doesn't make much sense for <code>MacroDef</code> in a proc-macro world</p>



<a name="180961089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180961089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180961089">(Nov 17 2019 at 15:18)</a>:</h4>
<p>Or, rather, we should rename current <code>MacroDef</code> to <code>DeclarativeMacroDef</code>, implment <code>HasSource</code> for that, and introduce a new enum for <code>MacroDef { proc, decl} </code></p>



<a name="180961092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180961092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180961092">(Nov 17 2019 at 15:18)</a>:</h4>
<p>Yeah.</p>



<a name="180961419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180961419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180961419">(Nov 17 2019 at 15:29)</a>:</h4>
<p>This also allows us to be more flexible if the declaration for compiler builtin macros changes.</p>



<a name="180966394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180966394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180966394">(Nov 17 2019 at 18:03)</a>:</h4>
<p>I presume that <code>Hygiene.def_crate</code> is <code>None</code> for proc macros?</p>



<a name="180969138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180969138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180969138">(Nov 17 2019 at 19:29)</a>:</h4>
<p>I actually don't know for sure, but I think <code>$crate</code> indeed doesn't have any special meaning for proc macros</p>



<a name="180969191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/180969191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#180969191">(Nov 17 2019 at 19:30)</a>:</h4>
<p>I suppose it can't make sense, given a proc macro crate can't export anything</p>



<a name="181027590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/181027590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#181027590">(Nov 18 2019 at 15:55)</a>:</h4>
<p>I'm just trying to implement the basic infrastructure for procedural macros, and I'm not sure what makes sense as a layout.<br>
Clearly, in <code>PerNs</code> we need an <code>AnyMacroDefId</code>, but I'm not sure whether it should be split by <code>procedural</code> vs <code>declarative</code> or <code>FnLike</code> / <code>Attribute</code>/ <code>Derive</code>. In terms of data structure sharing, it makes some sense to split on <code>procedural</code> vs <code>declarative</code> (so they can share the <code>AstId&lt;ast::MacroCall&gt;</code>, but in terms of functionality it makes sense to split by how they are used.</p>



<a name="181027714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/181027714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#181027714">(Nov 18 2019 at 15:57)</a>:</h4>
<p>This is just for name resolution - I'm not implementing expansion yet.<br>
Also, I'm not certain if we should check if a crate has <code>proc-macro=true</code> or just always check for the <code>proc_macro</code> attributes. I think it makes sense to check the crate type, but I'm not sure</p>



<a name="181034278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/181034278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#181034278">(Nov 18 2019 at 17:03)</a>:</h4>
<p>Ahah - this is a bit like <code>ImplItem</code></p>



<a name="181095207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding%20Watt%20Support/near/181095207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Hardcoding.20Watt.20Support.html#181095207">(Nov 19 2019 at 09:39)</a>:</h4>
<p>Is it possible to upcast an <code>AstId&lt;Something&gt;</code> to an <code>AstId&lt;An enum which contains Something&gt;</code> without an <code>AstDatabase</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>